<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/08/0008
 * Time: 15:30
 */

namespace app\sports_admin\controller;


use think\Request;
use app\sports_admin\model\SchoolYear as SchoolYearModel;
use app\sports_admin\service\SchoolYear as SchoolYearService;
use app\sports_admin\vaildate\SchoolYear as SchoolYearVaildate;
use app\sports_admin\model\School as SchoolModel;
class SchoolYear extends Base
{
    /*
     * 学年设置首页
     */
    public function index()
    {
        $data = SchoolModel::field('school_id,school_name')->select();
        return view('index',compact('data'));
    }
    /*
     * 获取学年信息
     * @param $page 第几页
     * @param $rows 每页显示的条数
     * @param $sort 要排序的字段
     * @param $sortOrder 升序/降序
     * @param $searchName 要检索的字段
     */
    public function getSchoolYearInfo()
    {
        $params = Request::instance()->only(['page','rows','sort','sortOrder','searchName','school']);
        $page = intval($params['page']) ? intval($params['page']) : 1;
        $rows = intval($params['rows']) ? intval($params['rows']) : 15;
        $sort = trim($params['sort']) ? trim($params['sort']) : 'id';
        $sortOrder = trim($params['sortOrder']) ? trim($params['sortOrder']) : 'asc';
        $searchName = trim($params['searchName']);
        $schoolId = intval($params['school']) ? $params['school'] : '';
        return SchoolYearModel::getSchoolYearInfo($page,$rows,$sort,$sortOrder,$searchName,$schoolId);
    }
    /*
     * 添加学年页面
     */
    public function add()
    {
        return view('add');
    }
    /*
     * 添加学年信息
     */
    public function insert(Request $request)
    {
        if($request->isAjax()){
            return SchoolYearService::insert();
        }
    }
    /*
     *编辑学年信息
     * @param $id 学年信息id
     */
    public function edit()
    {
        $id = input('get.id');
        $info = SchoolYearModel::where('id',$id)->find();
        return view('edit',compact('id','info'));
    }
    /*
     * 修改学年信息
     */
    public function update(Request $request){
        if($request->isAjax()){
            (new SchoolYearVaildate())->go_check();
            return SchoolYearService::update();
        }
    }
    /*
     * 删除学年信息
     */
    public function delete(Request $request)
    {
        if($request->isAjax()){
            (new SchoolYearVaildate())->go_check();
            return SchoolYearService::delete();
        }
    }
}